package com.magicborrow.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.activity.BorrowStuffDetailActivity;
import com.magicborrow.beans.ApplyEntity;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.enums.PriceModeEnum;
import com.magicborrow.utils.ImageLoadUtil;

import java.util.ArrayList;

/**
 * Created by ghost on 16/3/28.
 */
public class IBorrowedReceiveAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_FOOT = 0;
    private static final int TYPE_NORMAL = 1;
    private ArrayList<ApplyEntity.ContentData> mData;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public IBorrowedReceiveAdapter(Context pContext){
        this.mContext = pContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setData(ArrayList<ApplyEntity.ContentData> pData){
        this.mData = pData;
    }

    public ArrayList<ApplyEntity.ContentData> getData(){
        return this.mData;
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1 == getItemCount()){
            return TYPE_FOOT;
        }else{
            return TYPE_NORMAL;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_FOOT){
            View view = mLayoutInflater.inflate(R.layout.load_more_footer,null);
            view.setTag("foot");
            return new FootViewHolder(view);
        }else if(viewType == TYPE_NORMAL){
            View view = mLayoutInflater.inflate(R.layout.iborrowed_receive_item,null);
            return new IBorrowedReceiveAdapter.IBorrowedReceiveItemViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof IBorrowedReceiveItemViewHolder){
            IBorrowedReceiveItemViewHolder hold = (IBorrowedReceiveItemViewHolder)holder;
            final ApplyEntity.ContentData bean = mData.get(position);
            Log.e("ssss",Constants.ADDRESS+bean.getUser().getAvatar80());
            ImageLoadUtil.loadImage(hold.imv_head, Constants.ADDRESS+bean.getTarget().getAvatar80().split("\\?")[0],R.mipmap.head_default);
            String url = "";
            if(bean.getWare().getImages() != null && bean.getWare().getImages().size() > 0){
                url = Constants.ADDRESS+bean.getWare().getImages().get(0).getPreview();
            }
            ImageLoadUtil.loadImage(hold.good_pic,url,R.mipmap.img_default);
            hold.good_name.setText(bean.getWare().getName());
            hold.tv_name.setText(bean.getTarget().getNickname());
            hold.tv_address.setText("地址: "+bean.getWare().getAddress());
            hold.borrowTime.setText("借用时间: "+bean.getCreated());
            hold.borrowShichang.setText("借用时长: "+bean.getNumber()+ PriceModeEnum.getModeStr(bean.getWarePriceMode()));
            float lendScaleAVG = bean.getUser().getLendScaleAVG();//借出者的信用评分
            hold.commentCountMsg.setText(lendScaleAVG+"分");
            hold.rb_pingfen.setRating(lendScaleAVG);
            hold.shouldPay.setText("应付金额: "+(bean.getNumber()*bean.getWarePrice()+bean.getWareDeposit()));
            hold.rent.setText("实付租金: "+(bean.getNumber()*bean.getWarePrice()-bean.getDepositLoan()));
            hold.reloan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("data", bean.getWare());
                    intent.setClass(mContext, BorrowStuffDetailActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData.size()+1;
    }



    public class IBorrowedReceiveItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView imv_head;
        private TextView tv_name;
        private TextView statusMsg;
        private RatingBar rb_pingfen;
        private TextView commentCountMsg;
        private ImageView good_pic;
        private TextView good_name;
        private TextView borrowTime;
        private TextView borrowShichang;
        private TextView tv_address;
        private TextView shouldPay;
        private TextView reloan;
        private TextView rent;

        public IBorrowedReceiveItemViewHolder(View view) {
            super(view);
            imv_head = (ImageView)view.findViewById(R.id.imv_head);
            tv_name = (TextView)view.findViewById(R.id.tv_name);
            statusMsg = (TextView)view.findViewById(R.id.statusMsg);
            rb_pingfen = (RatingBar)view.findViewById(R.id.rb_pingfen);
            commentCountMsg = (TextView)view.findViewById(R.id.commentCountMsg);
            good_pic = (ImageView)view.findViewById(R.id.good_pic);
            good_name = (TextView)view.findViewById(R.id.good_name);
            borrowTime = (TextView)view.findViewById(R.id.borrowTime);
            borrowShichang = (TextView)view.findViewById(R.id.borrowShichang);
            tv_address = (TextView)view.findViewById(R.id.tv_address);
            shouldPay = (TextView)view.findViewById(R.id.shouldPay);
            reloan = (TextView)view.findViewById(R.id.reloan);
            rent = (TextView)view.findViewById(R.id.rent);
        }
    }

    public class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
